"""
SQL 查询管理
"""
from typing import Dict, Any
from db.connectors.mysql_connector import MySQLConnector
from db.connectors.postgres_connector import PostgreSQLConnector
from db.connectors.sqlalchemy_connector import SQLAlchemyConnector


class QueryManager:
    """
    SQL 查询管理
    """
    def __init__(self, db_type: str):
        """
        初始化查询管理器
        
        Args:
            db_type: 数据库类型 (mysql/postgres/sqlalchemy)
        """
        self.db_type = db_type
        self.connector = None

    def connect(self, connection_params: Dict[str, Any]) -> None:
        """
        连接数据库
        
        Args:
            connection_params: 连接参数
        """
        if self.db_type == "mysql":
            self.connector = MySQLConnector()
        elif self.db_type == "postgres":
            self.connector = PostgreSQLConnector()
        elif self.db_type == "sqlalchemy":
            self.connector = SQLAlchemyConnector()
        else:
            raise ValueError(f"不支持的数据库类型: {self.db_type}")
        
        self.connector.connect(connection_params)

    def execute_query(self, query: str) -> Dict[str, Any]:
        """
        执行 SQL 查询
        
        Args:
            query: SQL 查询语句
        """
        if not self.connector:
            raise RuntimeError("数据库未连接，请先调用 connect 方法")
        return self.connector.execute_query(query)

    def close(self) -> None:
        """
        关闭数据库连接
        """
        if self.connector:
            self.connector.close()